package com.ly.dingtalk.service.file.media;

import static com.ly.dingtalk.constant.DingUrlConstant.MEDIA_UPLOAD;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.util.Assert;

import com.aliyun.dingtalkstorage_1_0.models.AddFolderRequest;
import com.aliyun.dingtalkstorage_1_0.models.AddFolderResponse;
import com.aliyun.dingtalkstorage_1_0.models.AddSpaceHeaders;
import com.aliyun.dingtalkstorage_1_0.models.AddSpaceRequest;
import com.aliyun.dingtalkstorage_1_0.models.AddSpaceResponse;
import com.aliyun.dingtalkstorage_1_0.models.CopyDentriesResponse;
import com.aliyun.dingtalkstorage_1_0.models.CopyDentryHeaders;
import com.aliyun.dingtalkstorage_1_0.models.CopyDentryRequest;
import com.aliyun.dingtalkstorage_1_0.models.CopyDentryResponse;
import com.aliyun.dingtalkstorage_1_0.models.DeleteDentriesResponse;
import com.aliyun.dingtalkstorage_1_0.models.DeleteDentryHeaders;
import com.aliyun.dingtalkstorage_1_0.models.DeleteDentryRequest;
import com.aliyun.dingtalkstorage_1_0.models.DeleteDentryResponse;
import com.aliyun.dingtalkstorage_1_0.models.GetCurrentAppResponse;
import com.aliyun.dingtalkstorage_1_0.models.GetDentryOpenInfoRequest;
import com.aliyun.dingtalkstorage_1_0.models.GetDentryOpenInfoResponse;
import com.aliyun.dingtalkstorage_1_0.models.GetDentryRequest;
import com.aliyun.dingtalkstorage_1_0.models.GetDentryResponse;
import com.aliyun.dingtalkstorage_1_0.models.GetDentryThumbnailsResponse;
import com.aliyun.dingtalkstorage_1_0.models.GetOrgResponse;
import com.aliyun.dingtalkstorage_1_0.models.GetSpaceResponse;
import com.aliyun.dingtalkstorage_1_0.models.ListAllDentriesRequest;
import com.aliyun.dingtalkstorage_1_0.models.ListAllDentriesResponse;
import com.aliyun.dingtalkstorage_1_0.models.ListDentriesRequest;
import com.aliyun.dingtalkstorage_1_0.models.ListDentriesResponse;
import com.aliyun.dingtalkstorage_1_0.models.ListDentryVersionsHeaders;
import com.aliyun.dingtalkstorage_1_0.models.ListDentryVersionsRequest;
import com.aliyun.dingtalkstorage_1_0.models.ListDentryVersionsResponse;
import com.aliyun.dingtalkstorage_1_0.models.MoveDentriesResponse;
import com.aliyun.dingtalkstorage_1_0.models.MoveDentryHeaders;
import com.aliyun.dingtalkstorage_1_0.models.MoveDentryRequest;
import com.aliyun.dingtalkstorage_1_0.models.MoveDentryResponse;
import com.aliyun.dingtalkstorage_1_0.models.RenameDentryHeaders;
import com.aliyun.dingtalkstorage_1_0.models.RenameDentryRequest;
import com.aliyun.dingtalkstorage_1_0.models.RenameDentryResponse;
import com.aliyun.dingtalkstorage_1_0.models.RevertDentryVersionHeaders;
import com.aliyun.dingtalkstorage_1_0.models.RevertDentryVersionRequest;
import com.aliyun.dingtalkstorage_1_0.models.RevertDentryVersionResponse;
import com.aliyun.dingtalkstorage_2_0.models.CommitFileRequest;
import com.aliyun.dingtalkstorage_2_0.models.CommitFileResponse;
import com.aliyun.dingtalkstorage_2_0.models.GetFileUploadInfoRequest;
import com.aliyun.dingtalkstorage_2_0.models.GetFileUploadInfoResponse;
import com.aliyun.dingtalkstorage_2_0.models.SearchDentriesRequest;
import com.aliyun.dingtalkstorage_2_0.models.SearchDentriesResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.models.RuntimeOptions;
import com.dingtalk.api.request.OapiMediaUploadRequest;
import com.dingtalk.api.response.OapiMediaUploadResponse;
import com.google.common.base.Preconditions;
import com.ly.dingtalk.common.exception.DingApiException;
import com.ly.dingtalk.component.application.IDingApp;
import com.ly.dingtalk.pojo.file.MediaRequest;
import com.ly.dingtalk.service.AbstractDingNewApi;
import com.ly.dingtalk.service.file.IDingMediaHandler;
import com.taobao.api.FileItem;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

/**
 * @author wangzhen
 */
@Slf4j
public class DingMediaHandler extends AbstractDingNewApi implements IDingMediaHandler {

    public DingMediaHandler(@NonNull IDingApp app) {
        super(app);
    }

    @Override
    public OapiMediaUploadResponse upload(@NotNull MediaRequest request) throws IOException {
        final FileItem fileItem = request.toFileItem();
        Preconditions.checkArgument(fileItem.isValid(), "文件不合法");
        OapiMediaUploadRequest req = new OapiMediaUploadRequest();
        req.setType(request.getMediaType().name());
        req.setMedia(fileItem);
        return execute(
                MEDIA_UPLOAD,
                req,
                () -> "上传媒体文件"
        );
    }

    @Override
    public GetOrgResponse storageInfoByunionId(@NonNull String unionId, String corpId){
        return execute(com.aliyun.dingtalkstorage_1_0.Client.class, client->{
            GetOrgResponse orgWithOptions = null;
            try {
                com.aliyun.dingtalkstorage_1_0.models.GetOrgHeaders getOrgHeaders = new com.aliyun.dingtalkstorage_1_0.models.GetOrgHeaders();
                getOrgHeaders.xAcsDingtalkAccessToken = accessToken();
                com.aliyun.dingtalkstorage_1_0.models.GetOrgRequest getOrgRequest = new com.aliyun.dingtalkstorage_1_0.models.GetOrgRequest()
                        .setUnionId(unionId);
                orgWithOptions = client.getOrgWithOptions(StringUtils.defaultIfBlank(corpId, app.getCorpId()), getOrgRequest, getOrgHeaders, new com.aliyun.teautil.models.RuntimeOptions());
            } catch (Exception e) {
                log.error("获取企业存储的相关信息失败!", e);
                throw new DingApiException(e.getMessage());
            }
            return orgWithOptions;
        }, ()->"获取企业存储的相关信息");
    }

    @Override
    public GetCurrentAppResponse currentAppsStorageInfoByunionId(@NonNull String unionId) {
        return execute(com.aliyun.dingtalkstorage_1_0.Client.class, client->{
            GetCurrentAppResponse orgWithOptions = null;
            try {
                com.aliyun.dingtalkstorage_1_0.models.GetCurrentAppHeaders getCurrentAppHeaders = new com.aliyun.dingtalkstorage_1_0.models.GetCurrentAppHeaders();
                getCurrentAppHeaders.xAcsDingtalkAccessToken = accessToken();
                com.aliyun.dingtalkstorage_1_0.models.GetCurrentAppRequest getCurrentAppRequest = new com.aliyun.dingtalkstorage_1_0.models.GetCurrentAppRequest()
                        .setUnionId(unionId);
                orgWithOptions = client.getCurrentAppWithOptions(getCurrentAppRequest, getCurrentAppHeaders, new com.aliyun.teautil.models.RuntimeOptions());
            } catch (Exception e) {
                log.error("获取企业存储的相关信息失败!", e);
                throw new DingApiException(e.getMessage());
            }
            return orgWithOptions;
        }, ()->"获取当前引用存储的相关信息");
    }

    @Override
    public AddSpaceResponse addSpaceWithOptions(AddSpaceRequest req) {
        return execute(com.aliyun.dingtalkstorage_1_0.Client.class, client->{
            AddSpaceResponse orgWithOptions = null;
            try {
                AddSpaceHeaders addSpaceHeaders = new AddSpaceHeaders();
                addSpaceHeaders.xAcsDingtalkAccessToken = accessToken();
                orgWithOptions = client.addSpaceWithOptions(req, addSpaceHeaders, new com.aliyun.teautil.models.RuntimeOptions());
            } catch (Exception e) {
                log.error("添加空间失败!", e);
                throw new DingApiException(e.getMessage());
            }
            return orgWithOptions;
        }, ()->"添加空间");
    }

    @Override
    public GetSpaceResponse getSpaceInfoById(@NonNull String spaceId, @NonNull String unionId) {
        return execute(com.aliyun.dingtalkstorage_1_0.Client.class, client->{
            GetSpaceResponse orgWithOptions = null;
            try {
                com.aliyun.dingtalkstorage_1_0.models.GetSpaceHeaders getSpaceHeaders = new com.aliyun.dingtalkstorage_1_0.models.GetSpaceHeaders();
                getSpaceHeaders.xAcsDingtalkAccessToken = accessToken();
                com.aliyun.dingtalkstorage_1_0.models.GetSpaceRequest getSpaceRequest = new com.aliyun.dingtalkstorage_1_0.models.GetSpaceRequest()
                        .setUnionId(unionId);
                orgWithOptions = client.getSpaceWithOptions(spaceId, getSpaceRequest, getSpaceHeaders, new com.aliyun.teautil.models.RuntimeOptions());
            } catch (Exception e) {
                log.error("获取空间信息失败!", e);
                throw new DingApiException(e.getMessage());
            }
            return orgWithOptions;
        }, ()->"获取空间信息");
    }

    @Override
    public AddFolderResponse addfolders(@NonNull String spaceId, @NonNull String unionId,String parentId,AddFolderRequest req) {
        return execute(com.aliyun.dingtalkstorage_1_0.Client.class, client->{
            AddFolderResponse orgWithOptions = null;
            try {
                com.aliyun.dingtalkstorage_1_0.models.AddFolderHeaders addFolderHeaders = new com.aliyun.dingtalkstorage_1_0.models.AddFolderHeaders();
                addFolderHeaders.xAcsDingtalkAccessToken = accessToken();
                orgWithOptions =  client.addFolderWithOptions(spaceId, StringUtils.defaultIfBlank(parentId, "0"), req, addFolderHeaders, new com.aliyun.teautil.models.RuntimeOptions());
            } catch (Exception e) {
                log.error("空间中添加文件夹失败!", e);
                throw new DingApiException(e.getMessage());
            }
            return orgWithOptions;
        }, ()->"空间中添加文件夹");
    }

    @Override
    public CopyDentryResponse copyDentryWithOptions(@NonNull String fromSpaceId, @NonNull String fromDentryId,
            @NonNull String toSpaceId, @NonNull String toDentryId, @NonNull String unionId, String conflictStrategy) {
        return execute(com.aliyun.dingtalkstorage_1_0.Client.class, client->{
            CopyDentryResponse orgWithOptions = null;
            try {
                CopyDentryHeaders copyDentryHeaders = new CopyDentryHeaders();
                copyDentryHeaders.xAcsDingtalkAccessToken = accessToken();
                CopyDentryRequest.CopyDentryRequestOption option = new CopyDentryRequest.CopyDentryRequestOption()
                        .setConflictStrategy(StringUtils.defaultIfBlank(conflictStrategy, "AUTO_RENAME"));
                CopyDentryRequest copyDentryRequest = new CopyDentryRequest()
                        .setUnionId(unionId)
                        .setTargetSpaceId(toSpaceId)
                        .setTargetFolderId(StringUtils.defaultIfBlank(toDentryId, "0"))
                        .setOption(option);
                orgWithOptions =  client.copyDentryWithOptions(fromSpaceId, fromDentryId, copyDentryRequest, copyDentryHeaders, new RuntimeOptions());
            } catch (Exception e) {
                log.error("复制文件/文件夹失败!", e);
                throw new DingApiException(e.getMessage());
            }
            return orgWithOptions;
        }, ()->"复制文件/文件夹");
    }
    
    @Override
    public CopyDentriesResponse copyDentriesWithOptions(@NonNull String fromSpaceId,
            List<String> fromDentries, @NonNull String toSpaceId, @NonNull String toDentryId,
            @NonNull String unionId, String conflictStrategy) {
        Assert.notEmpty(fromDentries, "源文件/文件夹 id不能为空!");
        return execute(com.aliyun.dingtalkstorage_1_0.Client.class, client->{
            CopyDentriesResponse orgWithOptions = null;
            try {
                com.aliyun.dingtalkstorage_1_0.models.CopyDentriesHeaders copyDentriesHeaders = new com.aliyun.dingtalkstorage_1_0.models.CopyDentriesHeaders();
                copyDentriesHeaders.xAcsDingtalkAccessToken = accessToken();
                com.aliyun.dingtalkstorage_1_0.models.CopyDentriesRequest.CopyDentriesRequestOption option = new com.aliyun.dingtalkstorage_1_0.models.CopyDentriesRequest.CopyDentriesRequestOption()
                        .setConflictStrategy(StringUtils.defaultIfBlank(conflictStrategy, "AUTO_RENAME"));
                com.aliyun.dingtalkstorage_1_0.models.CopyDentriesRequest copyDentriesRequest = new com.aliyun.dingtalkstorage_1_0.models.CopyDentriesRequest()
                        .setUnionId(unionId)
                        .setTargetSpaceId(toSpaceId)
                        .setTargetFolderId(StringUtils.defaultIfBlank(toDentryId, "0"))
                        .setDentryIds(fromDentries)
                        .setOption(option);
                orgWithOptions = client.copyDentriesWithOptions(fromSpaceId, copyDentriesRequest, copyDentriesHeaders, new com.aliyun.teautil.models.RuntimeOptions());
            } catch (Exception e) {
                log.error("批量复制文件/文件夹失败!", e);
                throw new DingApiException(e.getMessage());
            }
            return orgWithOptions;
        }, ()->"批量复制文件/文件夹");
    }
    
    @Override
    public MoveDentryResponse moveDentryWithOptions(@NonNull String fromSpaceId,
            @NonNull String fromDentryId, @NonNull String toSpaceId, @NonNull String toDentryId,
            @NonNull String unionId, String conflictStrategy) {
        return execute(com.aliyun.dingtalkstorage_1_0.Client.class, client->{
            MoveDentryResponse orgWithOptions = null;
            try {
                MoveDentryHeaders moveDentryHeaders = new MoveDentryHeaders();
                moveDentryHeaders.xAcsDingtalkAccessToken = accessToken();
                MoveDentryRequest.MoveDentryRequestOption option = new MoveDentryRequest.MoveDentryRequestOption()
                        .setConflictStrategy(StringUtils.defaultIfBlank(conflictStrategy, "AUTO_RENAME"))
                        .setPresevePermissions(true);
                MoveDentryRequest moveDentryRequest = new MoveDentryRequest()
                        .setUnionId(unionId)
                        .setTargetSpaceId(toSpaceId)
                        .setTargetFolderId(StringUtils.defaultIfBlank(toDentryId, "0"))
                        .setOption(option);
                orgWithOptions =  client.moveDentryWithOptions(fromSpaceId, fromDentryId, moveDentryRequest, moveDentryHeaders, new RuntimeOptions());
            } catch (Exception e) {
                log.error("移动文件/文件夹失败!", e);
                throw new DingApiException(e.getMessage());
            }
            return orgWithOptions;
        }, ()->"移动复制文件/文件夹");
    }
    
    @Override
    public MoveDentriesResponse moveDentriesWithOptions(@NonNull String fromSpaceId,
            List<String> fromDentries, @NonNull String toSpaceId, @NonNull String toDentryId,
            @NonNull String unionId, String conflictStrategy) {
        Assert.notEmpty(fromDentries, "源文件/文件夹 id不能为空!");
        return execute(com.aliyun.dingtalkstorage_1_0.Client.class, client->{
            MoveDentriesResponse orgWithOptions = null;
            try {
                com.aliyun.dingtalkstorage_1_0.models.MoveDentriesHeaders moveDentriesHeaders = new com.aliyun.dingtalkstorage_1_0.models.MoveDentriesHeaders();
                moveDentriesHeaders.xAcsDingtalkAccessToken = accessToken();
                com.aliyun.dingtalkstorage_1_0.models.MoveDentriesRequest.MoveDentriesRequestOption option = new com.aliyun.dingtalkstorage_1_0.models.MoveDentriesRequest.MoveDentriesRequestOption()
                        .setConflictStrategy(StringUtils.defaultIfBlank(conflictStrategy, "AUTO_RENAME"))
                        .setPreservePermissions(true);
                com.aliyun.dingtalkstorage_1_0.models.MoveDentriesRequest moveDentriesRequest = new com.aliyun.dingtalkstorage_1_0.models.MoveDentriesRequest()
                        .setUnionId(unionId)
                        .setTargetSpaceId(toSpaceId)
                        .setTargetFolderId(StringUtils.defaultIfBlank(toDentryId, "0"))
                        .setDentryIds(fromDentries)
                        .setOption(option);
                orgWithOptions = client.moveDentriesWithOptions(fromSpaceId, moveDentriesRequest, moveDentriesHeaders, new com.aliyun.teautil.models.RuntimeOptions());
            } catch (Exception e) {
                log.error("批量移动文件/文件夹失败!", e);
                throw new DingApiException(e.getMessage());
            }
            return orgWithOptions;
        }, ()->"批量移动文件/文件夹");
    }
    
    @Override
    public RenameDentryResponse renameDentryWithOptions(@NonNull String unionId, @NonNull String newName,
            @NonNull String spaceId, @NonNull String dentryId) {
        return execute(com.aliyun.dingtalkstorage_1_0.Client.class, client->{
            RenameDentryResponse orgWithOptions = null;
            try {
                RenameDentryHeaders renameDentryHeaders = new RenameDentryHeaders();
                renameDentryHeaders.xAcsDingtalkAccessToken = accessToken();
                RenameDentryRequest renameDentryRequest = new RenameDentryRequest()
                        .setUnionId(unionId)
                        .setNewName(newName);
                orgWithOptions = client.renameDentryWithOptions(spaceId, dentryId, renameDentryRequest, renameDentryHeaders, new RuntimeOptions());
            } catch (Exception e) {
                log.error("重命名文件或文件夹失败!", e);
                throw new DingApiException(e.getMessage());
            }
            return orgWithOptions;
        }, ()->"重命名文件或文件夹");
    }
    
    @Override
    public DeleteDentryResponse deleteDentryWithOptions(@NonNull String unionId, Boolean toRecycleBin,
            @NonNull String spaceId, @NonNull String dentryId) {
        return execute(com.aliyun.dingtalkstorage_1_0.Client.class, client->{
            DeleteDentryResponse orgWithOptions = null;
            try {
                DeleteDentryHeaders deleteDentryHeaders = new DeleteDentryHeaders();
                deleteDentryHeaders.xAcsDingtalkAccessToken = accessToken();
                DeleteDentryRequest deleteDentryRequest = new DeleteDentryRequest()
                        .setToRecycleBin(ObjectUtils.defaultIfNull(toRecycleBin, true))
                        .setUnionId(unionId);
                orgWithOptions = client.deleteDentryWithOptions(spaceId, dentryId, deleteDentryRequest, deleteDentryHeaders, new RuntimeOptions());
            } catch (Exception e) {
                log.error("删除文件或文件夹失败!", e);
                throw new DingApiException(e.getMessage());
            }
            return orgWithOptions;
        }, ()->"删除文件或文件夹");
    }
    
    @Override
    public DeleteDentriesResponse deleteDentriesWithOptions(@NonNull String unionId,
                Boolean toRecycleBin, @NonNull String spaceId, List<String> dentriesId) {
        Assert.notEmpty(dentriesId,"文件/文件夹不能为空!");
        return execute(com.aliyun.dingtalkstorage_1_0.Client.class, client->{
            DeleteDentriesResponse orgWithOptions = null;
            try {
                com.aliyun.dingtalkstorage_1_0.models.DeleteDentriesHeaders deleteDentriesHeaders = new com.aliyun.dingtalkstorage_1_0.models.DeleteDentriesHeaders();
                deleteDentriesHeaders.xAcsDingtalkAccessToken = accessToken();
                com.aliyun.dingtalkstorage_1_0.models.DeleteDentriesRequest.DeleteDentriesRequestOption option = new com.aliyun.dingtalkstorage_1_0.models.DeleteDentriesRequest.DeleteDentriesRequestOption()
                        .setToRecycleBin(true);
                com.aliyun.dingtalkstorage_1_0.models.DeleteDentriesRequest deleteDentriesRequest = new com.aliyun.dingtalkstorage_1_0.models.DeleteDentriesRequest()
                        .setUnionId(unionId)
                        .setDentryIds(dentriesId)
                        .setOption(option);
                orgWithOptions = client.deleteDentriesWithOptions(spaceId, deleteDentriesRequest, deleteDentriesHeaders, new com.aliyun.teautil.models.RuntimeOptions());
            } catch (Exception e) {
                log.error("批量删除文件或文件夹失败!", e);
                throw new DingApiException(e.getMessage());
            }
            return orgWithOptions;
        }, ()->"批量删除文件或文件夹");
    }
    
    @Override
    public ListDentryVersionsResponse listDentryVersionsWithOptions(@NonNull String spaceId, @NonNull String dentryId,
            @NonNull String unionId, Integer maxResults, String nextToken) {
        return execute(com.aliyun.dingtalkstorage_1_0.Client.class, client->{
            ListDentryVersionsResponse orgWithOptions = null;
            try {
                ListDentryVersionsHeaders listDentryVersionsHeaders = new ListDentryVersionsHeaders();
                listDentryVersionsHeaders.xAcsDingtalkAccessToken = accessToken();
                ListDentryVersionsRequest listDentryVersionsRequest = new ListDentryVersionsRequest()
                        .setNextToken(nextToken)
                        .setMaxResults(maxResults)
                        .setUnionId(unionId);
                orgWithOptions = client.listDentryVersionsWithOptions(spaceId, dentryId, listDentryVersionsRequest, listDentryVersionsHeaders, new RuntimeOptions());
            } catch (Exception e) {
                log.error("获取文件版本列表失败!", e);
                throw new DingApiException(e.getMessage());
            }
            return orgWithOptions;
        }, ()->"获取文件版本列表");
    }

    @Override
    public RevertDentryVersionResponse revertDentryVersionWithOptions(@NonNull String spaceId,
            @NonNull String dentryId, @NonNull String unionId, @NonNull String version) {
        return execute(com.aliyun.dingtalkstorage_1_0.Client.class, client->{
            RevertDentryVersionResponse orgWithOptions = null;
            try {
                RevertDentryVersionHeaders revertDentryVersionHeaders = new RevertDentryVersionHeaders();
                revertDentryVersionHeaders.xAcsDingtalkAccessToken = accessToken();
                RevertDentryVersionRequest revertDentryVersionRequest = new RevertDentryVersionRequest()
                        .setUnionId(unionId);
                orgWithOptions = client.revertDentryVersionWithOptions(spaceId, dentryId, version, revertDentryVersionRequest, revertDentryVersionHeaders, new RuntimeOptions());
            } catch (Exception e) {
                log.error("恢复文件历史版本!", e);
                throw new DingApiException(e.getMessage());
            }
            return orgWithOptions;
        }, ()->"恢复文件历史版本");
    }

    @Override
    public ListDentriesResponse listDentriesWithOptions(@NonNull String spaceId, ListDentriesRequest req) {
        return execute(com.aliyun.dingtalkstorage_1_0.Client.class, client->{
            ListDentriesResponse orgWithOptions = null;
            try {
                com.aliyun.dingtalkstorage_1_0.models.ListDentriesHeaders listDentriesHeaders = new com.aliyun.dingtalkstorage_1_0.models.ListDentriesHeaders();
                listDentriesHeaders.xAcsDingtalkAccessToken = accessToken();
                orgWithOptions = client.listDentriesWithOptions(spaceId, req, listDentriesHeaders, new com.aliyun.teautil.models.RuntimeOptions());
            } catch (Exception e) {
                log.error("获取文件或文件夹列表!", e);
                throw new DingApiException(e.getMessage());
            }
            return orgWithOptions;
        }, ()->"获取文件或文件夹列表");
    }

    @Override
    public ListAllDentriesResponse listAllDentriesWithOptions(@NonNull String spaceId, ListAllDentriesRequest req) {
        return execute(com.aliyun.dingtalkstorage_1_0.Client.class, client->{
            ListAllDentriesResponse orgWithOptions = null;
            try {
                com.aliyun.dingtalkstorage_1_0.models.ListAllDentriesHeaders listAllDentriesHeaders = new com.aliyun.dingtalkstorage_1_0.models.ListAllDentriesHeaders();
                listAllDentriesHeaders.xAcsDingtalkAccessToken = accessToken();
                orgWithOptions = client.listAllDentriesWithOptions(spaceId, req, listAllDentriesHeaders, new com.aliyun.teautil.models.RuntimeOptions());
            } catch (Exception e) {
                log.error("获取空间下所有文件或文件夹列表!", e);
                throw new DingApiException(e.getMessage());
            }
            return orgWithOptions;
        }, ()->"获取空间下所有文件或文件夹列表");
    }

    @Override
    public GetDentryOpenInfoResponse getDentryOpenInfoWithOptions(@NonNull String spaceId,
            @NonNull String dentryId, GetDentryOpenInfoRequest req) {
        return execute(com.aliyun.dingtalkstorage_1_0.Client.class, client->{
            GetDentryOpenInfoResponse orgWithOptions = null;
            try {
                com.aliyun.dingtalkstorage_1_0.models.GetDentryOpenInfoHeaders getDentryOpenInfoHeaders = new com.aliyun.dingtalkstorage_1_0.models.GetDentryOpenInfoHeaders();
                getDentryOpenInfoHeaders.xAcsDingtalkAccessToken = accessToken();
                orgWithOptions = client.getDentryOpenInfoWithOptions(spaceId, dentryId, req, getDentryOpenInfoHeaders, new com.aliyun.teautil.models.RuntimeOptions());
            } catch (Exception e) {
                log.error("获取文件预览或编辑信息失败!", e);
                throw new DingApiException(e.getMessage());
            }
            return orgWithOptions;
        }, () -> "获取文件预览或编辑信息");
    }

    @Override
    public GetDentryThumbnailsResponse getDentryThumbnailsWithOptions(@NonNull String spaceId,
            List<String> dentriesId, @NonNull String unionId) {
        Assert.notEmpty(dentriesId,"文件/文件夹不能为空!");
        return execute(com.aliyun.dingtalkstorage_1_0.Client.class, client->{
            GetDentryThumbnailsResponse orgWithOptions = null;
            try {
                com.aliyun.dingtalkstorage_1_0.models.GetDentryThumbnailsHeaders getDentryThumbnailsHeaders = new com.aliyun.dingtalkstorage_1_0.models.GetDentryThumbnailsHeaders();
                getDentryThumbnailsHeaders.xAcsDingtalkAccessToken = accessToken();
                com.aliyun.dingtalkstorage_1_0.models.GetDentryThumbnailsRequest getDentryThumbnailsRequest = new com.aliyun.dingtalkstorage_1_0.models.GetDentryThumbnailsRequest()
                        .setUnionId(unionId)
                        .setDentryIds(dentriesId);
                orgWithOptions = client.getDentryThumbnailsWithOptions(spaceId, getDentryThumbnailsRequest, getDentryThumbnailsHeaders, new com.aliyun.teautil.models.RuntimeOptions());
            } catch (Exception e) {
                log.error("批量获取文件缩略图失败!", e);
                throw new DingApiException(e.getMessage());
            }
            return orgWithOptions;
        }, () -> "批量获取文件缩略图");
    }
    
    @Override
    public SearchDentriesResponse searchDentriesWithOptions(SearchDentriesRequest req) {
        return execute(com.aliyun.dingtalkstorage_2_0.Client.class, client->{
            SearchDentriesResponse orgWithOptions = null;
            try {
                com.aliyun.dingtalkstorage_2_0.models.SearchDentriesHeaders searchDentriesHeaders = new com.aliyun.dingtalkstorage_2_0.models.SearchDentriesHeaders();
                searchDentriesHeaders.xAcsDingtalkAccessToken = accessToken();
                orgWithOptions = client.searchDentriesWithOptions(req, searchDentriesHeaders, new com.aliyun.teautil.models.RuntimeOptions());
            } catch (Exception e) {
                log.error("搜索文件失败!", e);
                throw new DingApiException(e.getMessage());
            }
            return orgWithOptions;
        }, () -> "搜索文件");
    }
    

    /**
     * 
     * @Description: 你自己在空间中创建的文件夹 就有这个dentryUuid
     * @see com.ly.dingtalk.service.file.IDingMediaHandler#getFileUploadInfoWithOptions(java.lang.String, com.aliyun.dingtalkstorage_2_0.models.GetFileUploadInfoRequest)
     * @date 2023-12-28 10:43:27
     */
    @Override
    public GetFileUploadInfoResponse getFileUploadInfoWithOptions(@NonNull String parentDentryUuid, GetFileUploadInfoRequest req) {
        return execute( com.aliyun.dingtalkstorage_2_0.Client.class, client->{
            GetFileUploadInfoResponse orgWithOptions = null;
            try {
                com.aliyun.dingtalkstorage_2_0.models.GetFileUploadInfoHeaders getFileUploadInfoHeaders = new com.aliyun.dingtalkstorage_2_0.models.GetFileUploadInfoHeaders();
                getFileUploadInfoHeaders.xAcsDingtalkAccessToken = accessToken();
                orgWithOptions = client.getFileUploadInfoWithOptions(parentDentryUuid, req, getFileUploadInfoHeaders, new com.aliyun.teautil.models.RuntimeOptions());
            } catch (Exception e) {
                log.error("获取文件上传信息失败!", e);
                throw new DingApiException(e.getMessage());
            }
            return orgWithOptions;
        }, () -> "获取文件上传信息");
    }
    
    @Override
    public Boolean uploadFile(File file, Map<String, String> headers ,String resourceUrl) {
        Boolean result = true;
        try {
            URL url = new URL(resourceUrl);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            if (headers != null) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    connection.setRequestProperty(entry.getKey(), entry.getValue());
                }
            }
            connection.setDoOutput(true);
            connection.setRequestMethod("PUT");
            connection.setUseCaches(false);
            connection.setReadTimeout(10000);
            connection.setConnectTimeout(10000);
            connection.connect();
            OutputStream out = connection.getOutputStream();
            FileInputStream is = new FileInputStream(file);
            byte[] b =new byte[1024];
            int temp;
            while ((temp=is.read(b))!=-1){
                   out.write(b,0,temp);
            }
            out.flush();
            out.close();
            int responseCode = connection.getResponseCode();
            connection.disconnect();
            if (responseCode != 200) {
                result = false;
             }
        } catch (Exception e) {
            log.error("uploadFile报错!", e);
            result = false;
        }
        return result;
    }
    
    @Override
    public CommitFileResponse commitFileWithOptions(@NonNull String parentDentryUuid, CommitFileRequest req) {
        return execute( com.aliyun.dingtalkstorage_2_0.Client.class, client->{
            CommitFileResponse orgWithOptions = null;
            try {
                com.aliyun.dingtalkstorage_2_0.models.CommitFileHeaders commitFileHeaders = new com.aliyun.dingtalkstorage_2_0.models.CommitFileHeaders();
                commitFileHeaders.xAcsDingtalkAccessToken = accessToken();
                orgWithOptions = client.commitFileWithOptions(parentDentryUuid, req, commitFileHeaders, new com.aliyun.teautil.models.RuntimeOptions());
            } catch (Exception _err) {
                log.error("提交文件失败!", _err);
                TeaException err = new TeaException(_err.getMessage(), _err);
                if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                    log.error("code: {}, message: {}",err.code,err.message);
                }
                throw new DingApiException(_err.getMessage());
            }
            return orgWithOptions;
        }, () -> "提交文件");
    }
    
    @Override
    public GetDentryResponse getDentryWithOptions(@NonNull String spaceId, @NonNull String dentryId, GetDentryRequest req) {
        return execute( com.aliyun.dingtalkstorage_1_0.Client.class, client->{
            GetDentryResponse orgWithOptions = null;
            try {
                com.aliyun.dingtalkstorage_1_0.models.GetDentryHeaders getDentryHeaders = new com.aliyun.dingtalkstorage_1_0.models.GetDentryHeaders();
                getDentryHeaders.xAcsDingtalkAccessToken = accessToken();
                orgWithOptions = client.getDentryWithOptions(spaceId, dentryId, req, getDentryHeaders, new com.aliyun.teautil.models.RuntimeOptions());
            } catch (Exception _err) {
                log.error("获取文件/文件夹信息失败!", _err);
                throw new DingApiException(_err.getMessage());
            }
            return orgWithOptions;
        }, () -> "获取文件/文件夹信息");
    }
}
